package patrol.mobile.com.mobilepatrol.activity.yanghuke.niandufeiyong;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.TheFormAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.NianduFeiyongBean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by z on 2018/9/9.
 * Des: 年度费用Activity
 */

public class NianDuFeiyongActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.mySpinner)
    MySpinner mySpinner;
    @BindView(R.id.xiaoxiuFeiyong)
    RadioButton xiaoxiuFeiyong;
    @BindView(R.id.djcxFeiyong)
    RadioButton djcxFeiyong;
    @BindView(R.id.shqxFeiyong)
    RadioButton shqxFeiyong;
    @BindView(R.id.lszyFeiyong)
    RadioButton lszyFeiyong;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.niandufeiyong_title_radiogroup)
    RadioGroup niandufeiyong_title_radiogroup;

    private TheFormAdapter theFormAdapter;
    private List<String> strings;
    private String obType = "小修费用";
    private int yearPostion;
    private List<NianduFeiyongBean> nianduFeiyongBeans;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_niandufeiyong_layout;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setVisibility(View.VISIBLE);
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("年度费用合计");
    }

    @Override
    public void initView() {
        super.initView();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(theFormAdapter = new TheFormAdapter());
        strings = TimeUtils.getYearList();
    }

    @Override
    public void setListener() {
        iv_back.setOnClickListener(this);
        getHttpData(strings.get(yearPostion));
        mySpinner.setmDatas(strings, new MySpinner.CallBack() {
            @Override
            public void onSelect(int postion) {
                yearPostion = postion;
                getHttpData(strings.get(yearPostion));
            }
        });
        niandufeiyong_title_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.xiaoxiuFeiyong:
                        obType = "小修费用";
                        break;
                    case R.id.djcxFeiyong:
                        obType = "冬季除雪";
                        break;
                    case R.id.shqxFeiyong:
                        obType = "水毁抢险";
                        break;
                    case R.id.lszyFeiyong:
                        obType = "临时作业";
                        break;
                }
                getHttpData(strings.get(yearPostion));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }


    private void getHttpData(String year) {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("obType", obType);
        hashMap.put("year", year);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.YHK_NIANDUFEIYONG);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                Log.e("~~~~~", response);
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    nianduFeiyongBeans = new ArrayList<>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        NianduFeiyongBean nianduFeiyongBean = new Gson().fromJson(jsonArray.getString(i), NianduFeiyongBean.class);
                        nianduFeiyongBeans.add(nianduFeiyongBean);
                    }
                    theFormAdapter.setList(nianduFeiyongBeans);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
